package com.wanghua.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wanghua.mall.product.service.CategoryService;
import com.wanghua.mall.product.vo.AttrAndGroupVo;
import com.wanghua.mall.product.vo.AttrGroupResponseVo;
import org.springframework.web.bind.annotation.*;

import com.wanghua.mall.product.entity.AttrGroupEntity;
import com.wanghua.mall.product.service.AttrGroupService;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author wanghua
 * @email 2367279431@qq.com
 * @date 2021-08-23 16:10:06
 */
@RestController
@RequestMapping("/product/attrgroup")
public class AttrGroupController {

    @Resource
    private AttrGroupService attrGroupService;

    //获取分类下所有分组&关联属性
    @GetMapping("/getAllAttrGroupAndAttrByCategoryId/{categoryId}")
    public R getAllAttrGroupAndAttrByCategoryId(@PathVariable("categoryId") Long categoryId) {
        List<AttrAndGroupVo> items = attrGroupService.getAllAttrGroupAndAttrByCategoryId(categoryId);
        return R.ok().put("items", items);

    }

    /**
     * 根据分类id查询相关属性分组
     */
    @GetMapping("/list/{categoryId}")
    public R listByCategoryId(@RequestParam Map<String, Object> params,
                              @PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrGroupService.listByCategoryId(params, categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 查询属性分组完整信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        AttrGroupResponseVo attrGroup = attrGroupService.infoDetail(id);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除分组时，连带设置所关联的基本属性的分组id字段为0
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        attrGroupService.deleteDetail(ids);
        return R.ok();
    }


}
